import Vue from 'vue'
import VueRouter from 'vue-router'
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
    redirect: '/index',
    children: [
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName: "about" */ '../views/user/user.vue')
      },
      {
        path: '/index',
        name: 'index',
        component: () => import(/* webpackChunkName: "about" */ '../views/index/index.vue')
      },
      {
        path: '/invitations',
        name: 'invitations',
        component: () => import(/* webpackChunkName: "about" */ '../views/invitation/invitationList.vue')
      },
      {
        path: '/invitations/content',
        name: 'invitationContent',
        component: () => import(/* webpackChunkName: "about" */ '../views/invitation/invitationContent.vue')
      },
      {
        path: '/topics/list',
        name: 'AddTopic',
        component: () => import(/* webpackChunkName: "about" */ '../views/topic/topicList.vue')
      },
      {
        path: '/topics/add',
        name: 'topicsAdd',
        component: () => import(/* webpackChunkName: "about" */ '../views/topic/AddTopic.vue')
      },
      {
        path: '/map/invitations',
        name: 'invitationsMap',
        component: () => import(/* webpackChunkName: "about" */ '../views/data/invitationListInMap.vue')
      },
      {
        path: '/data',
        name: 'data',
        component: () => import(/* webpackChunkName: "about" */ '../views/data/data.vue')
      },
      {
        path: '/admin/info',
        name: 'info',
        component: () => import(/* webpackChunkName: "about" */ '../views/admin/info.vue')
      },
      {
        path: '/notice/add',
        name: 'noticeAdd',
        component: () => import(/* webpackChunkName: "about" */ '../views/notice/add.vue')
      },
      {
        path: '/notice/list',
        name: 'noticeList',
        component: () => import(/* webpackChunkName: "about" */ '../views/notice/list.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 其他页面则要有token才能放行 之后的网络请求中要拿这个token放入请求头中
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login') /* 没有用else if */
  next()
})

export default router /* 因为要挂载导航守卫 所以不能实时导出 */
